package com.wcy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.entity.UserCollection;
import com.wcy.entity.UserFeedback;
import com.wcy.entity.pageVo.ApiDescriptor;
import com.wcy.entity.pageVo.ColumnConstructor;
import com.wcy.entity.pageVo.PageData;
import com.wcy.entity.queryVo.QueryConvertor;
import com.wcy.service.IUserFeedbackService;
import com.wcy.util.JwtTokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/userFeedback")
public class UserFeedbackController {

    @Autowired
    private IUserFeedbackService userFeedbackService;
    @Autowired
    private JwtTokenUtil jwtUtil;

    @GetMapping("/page")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<UserFeedback>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                      @RequestParam(defaultValue = "10") int pageSize,
                                                      HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        Page<UserFeedback> page = userFeedbackService.page(new Page<>(pageNum, pageSize),  wrapper);
        return ResponseEntity.ok(page);
    }

    @GetMapping("/list")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<UserFeedback>> getList(HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        List<UserFeedback> list = userFeedbackService.list(wrapper);
        return ResponseEntity.ok(list);
    }

    @PostMapping("/add")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> add(@RequestBody UserFeedback userFeedback) {
        userFeedback.setCreatedAt(LocalDateTime.now());
        userFeedbackService.save(userFeedback);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/update")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> update(@RequestBody UserFeedback userFeedback) {
        userFeedbackService.updateById(userFeedback);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        userFeedbackService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getByResourceId/{resourceId}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<UserFeedback>> getByResourceId(@PathVariable Integer resourceId,
                                                              @RequestParam(defaultValue = "1") int pageNum,
                                                              @RequestParam(defaultValue = "10") int pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("resource_id", resourceId);
        return ResponseEntity.ok(userFeedbackService.page(Page.of(pageNum,pageSize),wrapper));
    }

    @GetMapping("/getBySelf")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<UserFeedback>> getBySelf(HttpServletRequest request,
                                                        @RequestParam(defaultValue = "1") int pageNum,
                                                        @RequestParam(defaultValue = "10") int pageSize) {
        Integer userId =  jwtUtil.getUserIdFromToken(jwtUtil.getToken(request));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        return ResponseEntity.ok(userFeedbackService.page(Page.of(pageNum,pageSize),wrapper));
    }

    @GetMapping("/user/page")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<UserFeedback>> getUserFeedbackPage(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                                                          @RequestParam(name = "pageSize",defaultValue = "10") int pageSize,
                                                                      HttpServletRequest request){
        Integer userId =  jwtUtil.getUserIdFromToken(jwtUtil.getToken(request));
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData,true).convert(true);
        wrapper.eq("user_id",userId);
        Page<UserFeedback> page = userFeedbackService.page(new Page<>(pageNum, pageSize),  wrapper);
        return ResponseEntity.ok(page);
    }

    @DeleteMapping("/user/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Boolean> deleteUserFeedback(@PathVariable Integer id,HttpServletRequest request) {
        Integer userId =  jwtUtil.getUserIdFromToken(jwtUtil.getToken(request));
        UserFeedback feedback = userFeedbackService.getById(id);
        if(feedback.getUserId().equals(userId)){
            userFeedbackService.removeById(id);
            return ResponseEntity.ok(true);
        }
        return ResponseEntity.ok(false);
    }
    @PostMapping("/user/add")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Boolean> addUserFeedback(@RequestBody UserFeedback userFeedback,HttpServletRequest request) {
        Integer userId =  jwtUtil.getUserIdFromToken(jwtUtil.getToken(request));
        userFeedback.setUserId(userId);
        userFeedback.setCreatedAt(LocalDateTime.now());
        return ResponseEntity.ok(userFeedbackService.save(userFeedback));
    }

    @PostMapping("/user/update")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Boolean> updateUserFeedback(@RequestBody UserFeedback userFeedback,HttpServletRequest request) {
        Integer userId =  jwtUtil.getUserIdFromToken(jwtUtil.getToken(request));
        userFeedback.setUserId(userId);
        userFeedback.setCreatedAt(LocalDateTime.now());
        return ResponseEntity.ok(userFeedbackService.save(userFeedback));
    }
}